package com.ygqh.baby.handler;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.model.YgShoppingCarModel;
import com.ygqh.baby.po.YgUserBalance;
import com.ygqh.baby.service.YgUserBalanceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * 〈〉
 *
 * @author guohao
 * @date 2018/12/28 13:36
 * @since 1.0.0
 */
@Component
public abstract class BaseCommitOrderHandler extends BaseShoppingCarHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private YgUserBalanceService ygUserBalanceService;

    public abstract Message checkProduct(List<YgShoppingCarModel> carList);


    public Message checkAccountPrice(BigDecimal accountPrice, BigDecimal totalPrice, Long userId) {
        if (accountPrice.compareTo(new BigDecimal(0)) > 0) {
            YgUserBalance balance = ygUserBalanceService.findByUserId(userId);
            // 账户余额小于当前是使用金额
            if (balance.getBalancePrice().compareTo(accountPrice) < 0) {
                return Message.error("账户余额有误，请重试。", null);
            }
            // 使用的虚拟金额大于订单的实际金额
            if (accountPrice.compareTo(totalPrice) > 0) {
                return Message.error("账户余额有误，请重试。", null);
            }
        }
        return Message.success(null);
    }

}
